package com.ayou.healthmanagement.controller;

import com.ayou.healthmanagement.dto.UserDto;
import com.ayou.healthmanagement.pojo.HealthUser;
import com.ayou.healthmanagement.utils.JwtUtils;
import com.ayou.healthmanagement.utils.Result;
import com.ayou.healthmanagement.service.HealthUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("user")
public class HealthUserController {

    @Autowired
    private HealthUserService healthUserService;

    @PostMapping("/import")
    public Result importUsers(@RequestParam("file") MultipartFile file) {
        return Result.success(healthUserService.importUsers(file));
    }

    @PostMapping("/exportUser")
    public void exportUser(@RequestBody Long[] ids, HttpServletResponse response){
        healthUserService.downLoad(ids,response);
    }

    @GetMapping("/getUserByUserPhone")
    public Result getUserByUserPhone(String userPhone){
        return healthUserService.getUserByUserPhone(userPhone);
    }

    @GetMapping("/updateImgUrl")
    public Result updateImgUrl(Integer id,String imgUrl){
        return healthUserService.updateImgUrl(id,imgUrl);
    }

    @GetMapping("/updatePassword")
    public Result updatePassword(HttpServletRequest request,String oldPassword,String newPassword){
        String token = request.getHeader("token");
        int userId = Integer.parseInt(JwtUtils.getId(token));
        return healthUserService.updatePassword(userId,oldPassword,newPassword);
    }

    @GetMapping("/resetPassword")
    public Result resetPassword(Integer userId,String password){
        return healthUserService.resetPassword(userId,password);
    }

    @PostMapping("/delUser")
    public Result delUser(@RequestBody Integer[] ids){
        return healthUserService.delUser(ids);
    }

    @PostMapping("/updateUser")
    public Result updateUser(@RequestBody HealthUser healthUser){
        return healthUserService.updateUser(healthUser);
    }

    @PostMapping("/getSysUserList")
    public Result getSysUserList(@RequestBody UserDto userDto){
        return healthUserService.getSysUserList(userDto);
    }

    @GetMapping("/getUserById")
    public Result getUserById(HttpServletRequest request){
        String token = request.getHeader("token");
        Integer userId = Integer.parseInt(JwtUtils.getId(token));
        return healthUserService.getUserById(userId);
    }

    @PostMapping("/login")
    public Result login(@RequestBody HealthUser healthUser, HttpServletRequest request){
        return healthUserService.login(healthUser);
    }

    @PostMapping("/register")
    public Result register(@RequestBody HealthUser healthUser){
        return healthUserService.register(healthUser);
    }
}
